package com.cuit.zuke.hotel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cuit.zuke.hotel.service.HotelBookService;
import com.cuit.zuke.hotel.model.hotel.HotelBook;
import com.cuit.zuke.common.util.Result;
import com.cuit.zuke.hotel.vo.hotel.HotelBookQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Api(tags = "酒店预约管理接口")
@RestController
@RequestMapping("/hotel/hotelBook")
public class HotelBookController {

    @Autowired
    private HotelBookService hotelBookService;

    // 查询所有的酒店预约信息
    @ApiOperation(value = "获取所有酒店预约信息")
    @GetMapping("findAll")
    public Result findAllHotelBook() {
        List<HotelBook> list = hotelBookService.list();
        return Result.ok().setData(list);
    }

    // 逻辑删除酒店预约信息
    @ApiOperation(value = "逻辑删除酒店预约信息")
    @DeleteMapping("{id}")
    public Result removeHotelBook(@PathVariable Long id) {
        boolean flag = hotelBookService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error("id值错误或已删除");
        }
    }

    // 条件查询带分页
    @ApiOperation(value = "条件查询带分页酒店预约信息")
    @PostMapping("findPageHotelBook/{current}/{limit}")
    public Result findPageHotelBook(@PathVariable long current,
                                   @PathVariable long limit,
                                   @RequestBody
                                           (required = false) HotelBookQueryVo hotelBookQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<HotelBook> page = new Page<>(current, limit);

        //构建条件
        QueryWrapper<HotelBook> wrapper = new QueryWrapper<HotelBook>();

        String orderId = hotelBookQueryVo.getOrderId();
        String zkcode = hotelBookQueryVo.getZkcode();
        Date bookingTime = hotelBookQueryVo.getBookingTime();

        if (!StringUtils.isEmpty(orderId)) {
            wrapper.like("order_id", orderId);
        }
        if (!StringUtils.isEmpty(zkcode)) {
            wrapper.eq("zkcode", zkcode);
        }
        if (!StringUtils.isEmpty(bookingTime)) {
            try{
                Calendar start = Calendar.getInstance();
                Calendar end = Calendar.getInstance();
                start.setTime(bookingTime);
                start.add(Calendar.DATE,-1);
                start.add(Calendar.HOUR,12);
                end.setTime(bookingTime);
                end.add(Calendar.DATE,1);
                end.add(Calendar.HOUR,-12);
                wrapper.le("booking_time", end)
                        .ge("booking_time",start);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        // 调用方法实现分页查询
        Page<HotelBook> pageHotelBook = hotelBookService.page(page, wrapper);

        // 返回结果
        return Result.ok().setData(pageHotelBook);
    }

    // 添加酒店预约信息
    @ApiOperation(value = "添加酒店预约信息")
    @PostMapping("saveHotelBook")
    public Result saveHotelBook(@RequestBody HotelBook hotelBook) {

        boolean save = hotelBookService.save(hotelBook);

        if (save) {
            return Result.ok();
        } else {
            return Result.error("添加错误");
        }
    }


    // 根据 id 获取酒店预约信息
    @ApiOperation(value = "根据id查询酒店预约信息")
    @GetMapping("getHotelBook/{id}")
    public Result getHotelBook(@PathVariable Long id) {
        HotelBook hotelBook = hotelBookService.getById(id);
        return Result.ok().setData(hotelBook);
    }

    // 修改酒店预约信息
    @ApiOperation(value = "修改酒店预约信息")
    @PostMapping("updateHotelBook")
    public Result updateHotelBook(@RequestBody HotelBook hotelBook) {
        boolean flag = hotelBookService.updateById(hotelBook);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error("修改失败");
        }
    }

    // 批量删除酒店预约设置
    @ApiOperation(value = "批量删除酒店预约信息")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHotelBook(@RequestBody List<Long> idList) {
        hotelBookService.removeByIds(idList);
        return Result.ok();
    }

}
